import { Sprite} from "pixi.js";
import { TexturePool } from "@/utils/TexturePool.js";
import { ImageManager } from "@/managers/ImageManager.js";

export class Sprite_Arrow extends Sprite{
    static texture=null;
   
    /**
     * @param {number} direct
     * 0 上
     * 1 右 
     * 2 下
     * 3 左
     */
    constructor(direct=0){
        let ctor=new.target;
        if(!ctor.texture){
            ctor.texture=TexturePool.newTexture();
            ImageManager.loadSystem('Arrow').then(source=>{
                let texture=this.constructor.texture;
                texture.source=source;
                texture.update();
                source.off('resize',texture.update,texture);
            });
        }
        if(ctor.texture.refCount==null){
            ctor.texture.refCount=0;
        }
        ++ctor.texture.refCount;
        super({
            anchor:0.5,
            texture:ctor.texture
        });
        this.rotation=Math.PI*direct/2
        this.direct=direct;
    }

    destroy(opts){
        if(this.destroyed)return;
        opts=opts||{children:true};
        if(opts.texture){
            opts.texture=false;
        }
        if(opts.textureSource){
            opts.textureSource=false;
        }
        let texture=this.constructor.texture;
        --texture.refCount;
        texture.off('update',this.onViewUpdate, this);
        if(texture.refCount<=0){
            texture.refCount=0;
            ImageManager.releaseSystem('Arrow');
            TexturePool.releaseTexture(texture);
        }
        super.destroy(opts);
    }

    activate(){
        this.visible=true;
    }

    deactive(){
        this.visible=false;
    }
}